//
//  NSString+SQLChain.h
//  SQL演练
//
//  Created by GR on 2017/10/29.
//  Copyright © 2017年 GR Harry. All rights reserved.
//

#import <Foundation/Foundation.h>

// strValue(name) -> @"'name'"
#define strValue(a) [NSString stringWithFormat:@"'%@'", @#a]
// str(name) -> @"name"
#define str(a) @#a
// num(12) -> @(12)
#define num(a) @(a)


typedef NSString *(^NSStringParamBlock)(id);
typedef NSString *(^NSStringStringParamBlock)(NSString *);
typedef NSString *(^NSStringThreeParamBlock)(NSString *, NSString *, NSString *);

// 外键约束
static inline NSString * foreign_key(NSString *constraintName, NSString *filedName, NSString *referenceTableName, NSString *primary_key) {
    return [NSString stringWithFormat:@"constraint %@ foreign key %@ references %@(%@)", constraintName, filedName, referenceTableName, primary_key];
}

static inline NSString *InParenthesis(NSString *content) {
    return [NSString stringWithFormat:@"(%@)", content];
}

@interface NSString (SQLChain)

/** 数据类型 **/
- (NSString *)integer;
- (NSString *)text;
- (NSString *)real;
- (NSString *)blob;

/** 约束 **/
- (NSString *)notnull;
- (NSString *)unique;
- (NSStringParamBlock)defaultValue;
- (NSString *)primary_key;
- (NSString *)autoincrement;

/** 连接符 **/
- (NSStringStringParamBlock)dot;
- (NSString *)as;
- (NSStringStringParamBlock)append;

/** 条件语句 **/
- (NSStringParamBlock)eq;
- (NSStringParamBlock)neq;
- (NSStringParamBlock)greater;
- (NSStringParamBlock)greaterEq;
- (NSStringParamBlock)less;
- (NSStringParamBlock)lessEq;

// @"name".eq(str(jack)).AND(str(score)).greater(num(60)) ->  @"name = 'jack' AND score > 60"
- (NSStringStringParamBlock)AND;
- (NSStringStringParamBlock)OR;
- (NSStringStringParamBlock)like;

- (NSString *)asc;
- (NSString *)desc;

/** 统计语句 **/
- (NSString *)count;
- (NSString *)avg;
- (NSString *)sum;
- (NSString *)max;
- (NSString *)min;

@end
